package com.ymatou.promotion.api.testcase;

import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.promotion.api.parameter.GetCouponBatchDetailBean;
import com.ymatou.promotion.api.service.GetCouponBatchDetailCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.CouponCallService;
import com.ymttest.database.sqlwapper.YmtCouponBatchWapper;

/**
 * 获取优惠券批次详情信息
 *
 * @author chenjiazhu
 */
@TestSuite
public class Ts_GetCouponBatchDetail {
    private static GetCouponBatchDetailBean getcouponbatchdetailBean;
    private static GetCouponBatchDetailCall getcouponbatchdetailCall;

    @BeforeClass
    public static void setUp() {
        Logger.createResultFile("获取优惠券批次详情信息");
    }

    @Before
    public void caseUp() {
        getcouponbatchdetailBean = new GetCouponBatchDetailBean();
        getcouponbatchdetailCall = new GetCouponBatchDetailCall();
    }

    @AfterClass
    public static void tearDown() {
        Logger.generateResult(getcouponbatchdetailCall.getOpurl());
    }

    @After
    public void caseDown() {
        Logger.end();
    }

    @Test
    @Category(P1.class)
    @TestCase
    public void Tc_GetCouponBatchDetail_001() {
        Logger.start(true, "获取全场优惠券批次详情信息");
        try {
            String batchCode = new CouponCallService()
                    .createSellerDefaultCoupon();

            Map item = setGetCouponBatchDetailBean(batchCode);

            getcouponbatchdetailCall.setData(getcouponbatchdetailBean);
            getcouponbatchdetailCall.callService();

            VerifyReturn(item);

        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P1.class)
    @TestCase
    public void Tc_GetCouponBatchDetail_002() {
        Logger.start(true, "获取商品优惠券批次详情信息");
        try {

            String batchCode = new CouponCallService()
                    .createSellerProductCoupon();

            Map item = setGetCouponBatchDetailBean(batchCode);

            getcouponbatchdetailCall.setData(getcouponbatchdetailBean);
            getcouponbatchdetailCall.callService();

            VerifyReturn(item);

        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P1.class)
    @TestCase
    public void Tc_GetCouponBatchDetail_003() {
        Logger.start(true, "获取洋码头PC端全场优惠券批次详情信息");
        try {

            String batchCode = new CouponCallService().createSellerCoupon("1");

            Map item = setGetCouponBatchDetailBean(batchCode);

            getcouponbatchdetailCall.setData(getcouponbatchdetailBean);
            getcouponbatchdetailCall.callService();

            VerifyReturn(item);

        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P1.class)
    @TestCase
    public void Tc_GetCouponBatchDetail_004() {
        Logger.start(true, "获取洋码头WAP端全场优惠券批次详情信息");
        try {
            String batchCode = new CouponCallService().createSellerCoupon("2");

            Map item = setGetCouponBatchDetailBean(batchCode);

            getcouponbatchdetailCall.setData(getcouponbatchdetailBean);
            getcouponbatchdetailCall.callService();

            VerifyReturn(item);

        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P1.class)
    @TestCase
    public void Tc_GetCouponBatchDetail_005() {
        Logger.start(true, "获取洋码头APP全场优惠券批次详情信息");
        try {
            String batchCode = new CouponCallService().createSellerCoupon("3");

            Map item = setGetCouponBatchDetailBean(batchCode);

            getcouponbatchdetailCall.setData(getcouponbatchdetailBean);
            getcouponbatchdetailCall.callService();

            VerifyReturn(item);

        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P1.class)
    @TestCase
    public void Tc_GetCouponBatchDetail_006() {
        Logger.start(true, "获取神器APP全场优惠券批次详情信息");
        try {
            String batchCode = new CouponCallService().createSellerCoupon("4");

            Map item = setGetCouponBatchDetailBean(batchCode);

            getcouponbatchdetailCall.setData(getcouponbatchdetailBean);
            getcouponbatchdetailCall.callService();

            VerifyReturn(item);

        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P1.class)
    @TestCase
    public void Tc_GetCouponBatchDetail_007() {
        Logger.start(true, "获取神器WAP全场优惠券批次详情信息");
        try {
            String batchCode = new CouponCallService().createSellerCoupon("5");

            Map item = setGetCouponBatchDetailBean(batchCode);

            getcouponbatchdetailCall.setData(getcouponbatchdetailBean);
            getcouponbatchdetailCall.callService();

            VerifyReturn(item);

        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P1.class)
    @TestCase
    public void Tc_GetCouponBatchDetail_008() {
        Logger.start(true, "获取洋码头PC端商品优惠券批次详情信息");
        try {

            String batchCode = new CouponCallService()
                    .createSellerProductCoupon("1");

            Map item = setGetCouponBatchDetailBean(batchCode);

            getcouponbatchdetailCall.setData(getcouponbatchdetailBean);
            getcouponbatchdetailCall.callService();

            VerifyReturn(item);

        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P1.class)
    @TestCase
    public void Tc_GetCouponBatchDetail_009() {
        Logger.start(true, "获取洋码头WAP端商品优惠券批次详情信息");
        try {
            String batchCode = new CouponCallService()
                    .createSellerProductCoupon("2");

            Map item = setGetCouponBatchDetailBean(batchCode);

            getcouponbatchdetailCall.setData(getcouponbatchdetailBean);
            getcouponbatchdetailCall.callService();

            VerifyReturn(item);

        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P1.class)
    @TestCase
    public void Tc_GetCouponBatchDetail_010() {
        Logger.start(true, "获取洋码头APP商品优惠券批次详情信息");
        try {
            String batchCode = new CouponCallService()
                    .createSellerProductCoupon("3");

            Map item = setGetCouponBatchDetailBean(batchCode);

            getcouponbatchdetailCall.setData(getcouponbatchdetailBean);
            getcouponbatchdetailCall.callService();

            VerifyReturn(item);

        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P1.class)
    @TestCase
    public void Tc_GetCouponBatchDetail_011() {
        Logger.start(true, "获取神器APP商品优惠券批次详情信息");
        try {
            String batchCode = new CouponCallService()
                    .createSellerProductCoupon("4");

            Map item = setGetCouponBatchDetailBean(batchCode);

            getcouponbatchdetailCall.setData(getcouponbatchdetailBean);
            getcouponbatchdetailCall.callService();

            VerifyReturn(item);

        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P1.class)
    @TestCase
    public void Tc_GetCouponBatchDetail_012() {
        Logger.start(true, "获取神器WAP商品优惠券批次详情信息");
        try {
            String batchCode = new CouponCallService()
                    .createSellerProductCoupon("5");

            Map item = setGetCouponBatchDetailBean(batchCode);

            getcouponbatchdetailCall.setData(getcouponbatchdetailBean);
            getcouponbatchdetailCall.callService();

            VerifyReturn(item);

        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P1.class)
    @TestCase
    public void Tc_GetCouponBatchDetail_013() {
        Logger.start(false, "验证BatchCode空时报错");
        try {
            Map item = setGetCouponBatchDetailBean();
            getcouponbatchdetailBean.setBatchCode("");

            getcouponbatchdetailCall.setData(getcouponbatchdetailBean);
            getcouponbatchdetailCall.callService();

            Logger.verifyEquals("201", getcouponbatchdetailCall.getCode(),
                    "验证返回code");
            Logger.verifyEquals("-1", getcouponbatchdetailCall.getBCode(),
                    "验证返回Bcode");
            Logger.verifyEquals("-1:未查询到批次详情",
                    getcouponbatchdetailCall.getMsg(), "验证返回Msg");

        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P1.class)
    @TestCase
    @Ignore
    public void Tc_GetCouponBatchDetail_014() {
        Logger.start(false, "验证CreateUserId空时报错-业务有问题");
        try {

            Map item = setGetCouponBatchDetailBean();
            getcouponbatchdetailBean.setCreateUserId(0);

            getcouponbatchdetailCall.setData(getcouponbatchdetailBean);
            getcouponbatchdetailCall.callService();

            Logger.verifyEquals("201", getcouponbatchdetailCall.getCode(),
                    "验证返回code");
            Logger.verifyEquals("-1", getcouponbatchdetailCall.getBCode(),
                    "验证返回Bcode");
            Logger.verifyEquals("-1:未查询到批次详情",
                    getcouponbatchdetailCall.getMsg(), "验证返回MessageText");

        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P1.class)
    @TestCase
    public void Tc_GetCouponBatchDetail_015() {
        Logger.start(false, "验证BatchCode与CreateUserId不匹配时报错");
        try {
            String batchCode = new CouponCallService()
                    .createSellerDefaultCoupon();

            Map item = setGetCouponBatchDetailBean(batchCode);
            getcouponbatchdetailBean.setCreateUserId(111);

            getcouponbatchdetailCall.setData(getcouponbatchdetailBean);
            getcouponbatchdetailCall.callService();

            Logger.verifyEquals("201", getcouponbatchdetailCall.getCode(),
                    "验证返回code");
            Logger.verifyEquals("-1", getcouponbatchdetailCall.getBCode(),
                    "验证返回Bcode");
            Logger.verifyEquals("-1:未查询到批次详情",
                    getcouponbatchdetailCall.getMsg(), "验证返回MessageText");

        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    /**
     * 根据batchCode到Ymt_CouponBatch表中查询相应数据来设置请求bean
     * 
     * @param batchCode
     * @return
     */
    private Map setGetCouponBatchDetailBean(String batchCode) {
        Logger.comment("根据batchCode到Ymt_CouponBatch表中查询相应数据来设置请求bean");

        Map item = new YmtCouponBatchWapper().getCouponBatchByBatchCode(
                batchCode).get(0);

        getcouponbatchdetailBean.setBatchCode(batchCode);
        getcouponbatchdetailBean.setCreateUserId(Integer.parseInt(item.get(
                "iOperatorId").toString()));

        return item;
    }

    /**
     * 获取Ymt_CouponBatch表中最新一条数据来设置请求bean
     * 
     * @return
     */
    private Map setGetCouponBatchDetailBean() {
        Logger.comment("获取Ymt_CouponBatch表中最新一条数据来设置请求bean");

        Map item = new YmtCouponBatchWapper().getTop1CouponBatch().get(0);

        getcouponbatchdetailBean
                .setBatchCode(item.get("sBatchCode").toString());
        getcouponbatchdetailBean.setCreateUserId(Integer.parseInt(item.get(
                "iOperatorId").toString()));

        return item;
    }

    private void VerifyReturn(Map item) {
        Logger.verifyEquals("200", getcouponbatchdetailCall.getCode(),
                "验证返回code");
        Logger.verifyEquals("0", getcouponbatchdetailCall.getBCode(),
                "验证返回Bcode");
        Logger.verifyEquals("获取优惠券批次详情成功",
                getcouponbatchdetailCall.getMessageText(), "验证返回MessageText");

        String batchCode = getcouponbatchdetailBean.getBatchCode();

        Logger.verifyEquals(item.get("sBatchCode").toString(),
                getcouponbatchdetailCall.getBatchCode(),
                "验证返回BatchCode与数据库中表Ymt_CouponBatch中sBatchCode=" + batchCode
                        + "记录中sBatchCode字段相同");

        Logger.verifyEquals(item.get("sBatchPrefix").toString(),
                getcouponbatchdetailCall.getBatchPrefix(),
                "验证返回BatchPrefix与数据库中表Ymt_CouponBatch中sBatchCode=" + batchCode
                        + "记录中sBatchPrefix字段相同");

        Logger.verifyEquals(item.get("iOperatorId").toString(),
                getcouponbatchdetailCall.getSellerId(),
                "验证返回SellerId与数据库中表Ymt_CouponBatch中sBatchCode=" + batchCode
                        + "记录中iOperatorId字段相同");

        Logger.verifyEquals(item.get("sCouponName").toString(),
                getcouponbatchdetailCall.getCouponName(),
                "验证返回CouponName与数据库中表Ymt_CouponBatch中sBatchCode=" + batchCode
                        + "记录中sCouponName字段相同");

        Logger.verifyEquals(item.get("sApplyMemo").toString(),
                getcouponbatchdetailCall.getCouponDesc(),
                "验证返回CouponDesc与数据库中表Ymt_CouponBatch中sBatchCode=" + batchCode
                        + "记录中sApplyMemo字段相同");

        String fCouponValue = item.get("fCouponValue").toString()
                .replaceAll("[.]$", "");
        Logger.verifyEquals(
                fCouponValue.substring(0, fCouponValue.length() - 3),
                getcouponbatchdetailCall.getCouponValue(),
                "验证返回CouponValue与数据库中表Ymt_CouponBatch中sBatchCode=" + batchCode
                        + "记录中fCouponValue字段相同");

        String fMinOrderValue = item.get("fMinOrderValue").toString();
        Logger.verifyEquals(
                fMinOrderValue.substring(0, fMinOrderValue.length() - 3),
                getcouponbatchdetailCall.getOrderValue(),
                "验证返回OrderValue与数据库中表Ymt_CouponBatch中sBatchCode=" + batchCode
                        + "记录中fMinOrderValue字段相同");

        String iMaxUseTime = item.get("iMaxUseTime").toString();
        Logger.verifyEquals(iMaxUseTime,
                getcouponbatchdetailCall.getMaxSendNum(),
                "验证返回MaxSendNum与数据库中表Ymt_CouponBatch中sBatchCode=" + batchCode
                        + "记录中iMaxUseTime字段相同");

        String iMaxUseTimePerUser = item.get("iMaxUseTimePerUser").toString();
        Logger.verifyEquals(iMaxUseTimePerUser,
                getcouponbatchdetailCall.getUserMaxReceiveNum(),
                "验证返回UserMaxReceiveNum与数据库中表Ymt_CouponBatch中sBatchCode="
                        + batchCode + "记录中iMaxUseTimePerUser字段相同");

    }
}
